package com.tan.androidtest.customview.canvas;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.tan.androidtest.R;

/**
 * 测试canvas对绘制的辅助
 */

public class DemoView3 extends View {

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_test1);
    private Path path = new Path();


    public DemoView3(Context context) {
        super(context);
    }

    public DemoView3(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DemoView3(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path.addCircle(200, 200, 200, Path.Direction.CW);
        //1 范围裁切

        //canvas.save();
        //canvas.clipRect(50, 50, 400, 400);
        //canvas.clipPath(path);
        //canvas.drawBitmap(bitmap, 0, 0, mPaint);
        //canvas.restore();


        //2 几何变换
        //2.1 使用Canvas做常见的几何变换
        //canvas.save();
        //canvas.translate(100, 0);//平移
        //canvas.rotate(80);//旋转
        //canvas.scale(1.3f, 1.3f, bitmap.getWidth() / 2, bitmap.getHeight() / 2);放大缩小
        //canvas.skew(0.2f, 0);
        //canvas.drawBitmap(bitmap, 150, 150, mPaint);
        //canvas.restore();

        //2.2.使用Matrix来做自定义变换
        /*Matrix matrix = new Matrix();
        float pointsSrc[] = {0, 0, 200, 0, 0, 200, 200, 200};
        float pointsDst[] = {0 - 10, 0 + 50, 200 + 120, 0 - 90, 0 + 20, 200 + 30, 200 + 20, 200 + 60};

        matrix.reset();
        matrix.setPolyToPoly(pointsSrc, 0, pointsDst, 0, 4);

        canvas.save();
        canvas.concat(matrix);
        canvas.drawBitmap(bitmap, 0, 0, mPaint);
        canvas.restore();*/


        //2.3 使用Camera来做三维变换
        float centerX = 150 + bitmap.getWidth() / 2;
        float centerY = 150 + bitmap.getHeight() / 2;
        /*canvas.save();
        Camera camera = new Camera();
        camera.save();
        canvas.translate(-centerX, -centerY);
        camera.rotateY(30);
        camera.applyToCanvas(canvas);
        canvas.translate(centerX, centerY);
        camera.restore();
        canvas.drawBitmap(bitmap, 150, 150, mPaint);
        canvas.restore();*/

        /*Camera camera = new Camera();
        canvas.save();
        camera.save(); // 保存 Camera 的状态
        camera.rotateX(30); // 旋转 Camera 的三维空间
        canvas.translate(centerX, centerY); // 旋转之后把投影移动回来
        camera.applyToCanvas(canvas); // 把旋转投影到 Canvas
        //注意当通知有两个canvas的基础的操作的时候,其执行顺序是代码在后面的先执行,所以这句代码在 canvas.translate(centerX, centerY); 的后面
        canvas.translate(-centerX, -centerY); // 旋转之前把绘制内容移动到轴心（原点）
        camera.restore(); // 恢复 Camera 的状态*/

        canvas.drawBitmap(bitmap, 150, 150, mPaint);
        //canvas.restore();


    }


}
